//
//  CHSingleChioseView.m
//  CHOCTool
//
//  Created by cyptt on 2020/9/13.
//  Copyright © 2020 com.qingtiantree. All rights reserved.
// 2022

#import "CHSingleChioseView.h"

@interface CHSingleChioseView (){
    CGFloat selfW;
    CGFloat selfH;
}

@property(nonatomic,strong)UIButton * currentBtn;

@property(nonatomic,strong)NSMutableArray * buttonArr;
@property(nonatomic,strong)NSMutableArray * lineArr;
@property(nonatomic,assign)BOOL isShowView;




@end
static NSInteger SigleChioseTAG = 10000;
@implementation CHSingleChioseView


-(NSMutableArray *)buttonArr{
    if (!_buttonArr) {
        _buttonArr = [NSMutableArray array];
    }
    return _buttonArr;
}
-(NSMutableArray *)lineArr{
    if (!_lineArr) {
        _lineArr =[NSMutableArray array];
    }
    return _lineArr;
}
-(void)awakeFromNib{
    
    [super awakeFromNib];
    [self initViews];
}
- (instancetype)initWithCoder:(NSCoder *)coder
{
    self = [super initWithCoder:coder];
    if (self) {
         [self initViews];
    }
    return self;
}
- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        
         [self initViews];
    }
    return self;
}


-(void)initViews{
    self.isShowView = NO;
    selfW = self.frame.size.width;
    selfH = self.frame.size.height;
    
//    self.fontSize = 14 ; //默认字体大戏哦啊14
    self.titleFont = [UIFont systemFontOfSize:14];
    self.lineWith = 0 ;
    self.lineH = 2;
    self.islineBorder = YES;
   
}

-(void)layoutSubviews{
    [super layoutSubviews];
    selfW = self.frame.size.width;
    selfH = self.frame.size.height;
   
    //保证只加载一次
    if (self.isShowView == YES) {
        [self showSelectView];
    }
    
    self.isShowView = NO;
}

-(void)btnClickBlockFun:(void (^)(NSString * title ))btnClickBlock
{
    _btnClickBlock = btnClickBlock;
}

-(void)btnClickIndexBlockFun:(void (^)(NSString * title,int index))btnClickBlock{
    _btnClickIndexBlock = btnClickBlock;
}

-(void)setIndex:(int)index{
    
    UIButton * selctBtn = self.buttonArr[index];
    [self btnClick:selctBtn];
  
   

}

-(void)setTitleArr:(NSArray *)titleArr defaultColor:(UIColor *)defaultColor selectedColor:(UIColor *)selectedColor lineColor:(UIColor *)lineColor{
    
    _titleArr = titleArr;

    _defaultColor = defaultColor;
    _selectedColor = selectedColor;
    _lineColor = lineColor;

    self.isShowView = YES;

}

-(void)showSelectView{
    for (int i =0 ; i<self.titleArr.count; i++) {
        UIButton * button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(selfW*i/self.titleArr.count, 0, selfW/self.titleArr.count, selfH-10);
        [button setTitle:self.titleArr[i] forState:UIControlStateNormal];
        [button setTitleColor:self.defaultColor forState:UIControlStateNormal];
        [button setTitleColor:self.selectedColor forState:UIControlStateSelected];
        
        if (i==0) {
            button.selected = YES;
            self.currentBtn = button;
        }
        [button addTarget:self action:@selector(btnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self addSubview:button];
        [self.buttonArr addObject:button];
        
        
        button.tag =SigleChioseTAG +i;
        
        button.titleLabel.font = self.titleFont;
        
        
        CGFloat lineW = self.lineWith != 0?self.lineWith: [self getWidthWithString:self.titleArr[i] font:self.titleFont];
        UIView * lineView = [[UILabel alloc]initWithFrame:CGRectMake(0, selfH-10+2, lineW, self.lineH)];
    
        lineView.center = CGPointMake(selfW*i/self.titleArr.count+selfW/(2*self.titleArr.count), selfH-(self.lineH/2));
        lineView.backgroundColor = self.lineColor;
        if (self.islineBorder == YES) {
            lineView.layer.cornerRadius = self.lineH/2;
            lineView.layer.masksToBounds = YES;
        }
      
        
        if (button.selected) {
            lineView.hidden = NO;
        }else{
            lineView.hidden = YES;
        }
    
        [self addSubview:lineView];
        [self.lineArr addObject:lineView];
        
    }
}

-(void)btnClick:(UIButton *)btn{
    self.currentBtn.selected = NO;
    btn.selected = !btn.selected;
    self.currentBtn = btn;
  
    int index = (int)(btn.tag-SigleChioseTAG);
    for (UIView * lineView in self.lineArr) {
      
        if ([lineView isEqual:self.lineArr[index]]) {
            lineView.hidden= NO;
         
        }else{
           
            lineView.hidden = YES;
        }
    }
   
 
    if (self.btnClickBlock) {
        self.btnClickBlock(btn.currentTitle);
    }
    
    if (self.btnClickIndexBlock) {
        self.btnClickIndexBlock(btn.currentTitle,(int)index );
    }


}

-(void)hideLineView{
    for (UIView * lineView in self.lineArr) {
        lineView.backgroundColor = [UIColor clearColor];
    }
    
    int i =0;
    for (UIButton * button in self.buttonArr) {
      
        button.frame = CGRectMake(selfW*i/self.titleArr.count, 0, selfW/self.titleArr.count, selfH);
          ++i;
    }
}



-(CGFloat)getWidthWithString:(NSString *)string font:(UIFont *)font{
    NSDictionary *attrs = @{NSFontAttributeName:font};
    CGFloat width = [string boundingRectWithSize:CGSizeZero options:NSStringDrawingUsesLineFragmentOrigin attributes:attrs context:nil].size.width;
    return width;
}
@end
